package com.yan.test.work.testpaging3

import android.content.Context
import androidx.lifecycle.LifecycleCoroutineScope
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import androidx.paging.cachedIn
import kotlinx.coroutines.flow.Flow

/**
 * Create By : 'YWY'
 * On : 2024/4/6
 */
class OSCViewModel : ViewModel(){

    fun getDataFlow(ctx: Context , keyWord:String) : Flow<PagingData<DataBean>> =
        Pager<Int , DataBean>(
            config = PagingConfig(
                pageSize = 10,
                prefetchDistance = 1,
                enablePlaceholders = false,
                initialLoadSize = 10,
            ),
            pagingSourceFactory = { OSCPagingDataSource( ctx , keyWord ) },
            initialKey = 0
        ).flow.apply { cachedIn(viewModelScope) }

}